<template>
  <div class="comment">
    <div class="addcomment" v-show="!isFocus">
      <input type="text" placeholder="写跟帖" @focus="focusFn" />
      <span @click="toPage" class="comment">
        <i class="iconfont iconpinglun-"></i>
        <!-- 动态渲染评论数量 -->
        <em>{{ article.comment_length }}</em>
      </span>
      <!-- 点击收藏 -->
      <i @click="show_collect" :class="{reds: this.article.has_star}" class="iconfont iconshoucang"></i>
      <i class="iconfont iconfenxiang"></i>
    </div>
    <div class="inputcomment" v-show="isFocus">
      <textarea v-model="content" ref="commtext" rows="5"></textarea>
      <div>
        <span @click="sendFn">发 送</span>
        <span>取 消</span>
      </div>
    </div>
  </div>
</template>

<script>
import { pulish } from "../api/news";
import { postCollect } from '../api/news'
export default {
  // 在子组件中定义变量接收数据
  props: {
    article: {
      type: Object,
      require: true,

    },
  },
  data() {
    return {
      content: "",
      isFocus: false,
    };
  },
  methods: {
    // 点击收藏
    show_collect() {
      // console.log("收藏成功");
      postCollect(this.article.id).then(res=>{
        console.log(46,res);
        this.$toast.success(res.data.message)
        this.article.has_star = !this.article.has_star

      })
    },
    // 跳转到评论列表
    toPage() {
      if (location.href.split("/")[4].split("?")[0] == "articleDetail") {
        this.$router.push({
          path: "/comment",
          query: {
            // 传文章id到评论列表
            id: this.article.id,
          },
        });
        return;
      }
    },
    // 显示多行文本信息
    focusFn() {
      (this.isFocus = true),
        this.$nextTick(() => {
          this.$refs.commtext.focus();
        });
    },
    // 发表评论
    sendFn() {
      let obj = {
        content: this.content,
      };
      // 当parent_id存在时，再向obj添加parent_id属性
      if (this.parent_id) {
        obj.parent_id = this.parent_id;
      }
      pulish(this.article.id, obj).then((res) => {
        console.log(64, res);
        // 评论发表成功 更新评论列表
        if (res.data.message == "评论发布成功") {
          // 通知父组件需要更新评论列表数据了
          this.$emit("update");

        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.comment {
  position: fixed;
  bottom: 0;
  width: 100%;
}
.inputcomment {
  padding: 10px;
  box-sizing: border-box;
  width: 100%;
  display: flex;
  background-color: #fff;
  align-items: flex-end;
  textarea {
    flex: 3;
    background-color: #eee;
    border: none;
    border-radius: 10px;
    padding: 10px;
  }
  div {
    padding: 20px;
  }
  span {
    display: block;
    flex: 1;
    height: 24px;
    line-height: 24px;
    padding: 0 10px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 6px;
    font-size: 13px;
  }
}
.addcomment {
  background-color: #fff;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  margin-top: 20px;
  display: flex;
  text-align: center;
  position: absolute;
  bottom: 0;
  left: 0;
  > input {
    flex: 4;
    height: 30px;
    line-height: 30px;
    border-radius: 15px;
    border: none;
    background-color: #eee;
    padding-left: 20px;
    font-size: 14px;
  }
  i {
    font-size: 20px;
  }
  > span {
    flex: 1;
    position: relative;
    > em {
      position: absolute;
      right: 0;
      top: -5px;
      font-size: 10px;
      background-color: #f00;
      color: #fff;
      border-radius: 5px;
      padding: 3px 5px;
    }
  }
  .reds{
    color: red;
  }
  > i {
    flex: 1;
  }
}
</style>